package com.fun.demo.fun.kayang;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class SMTP {

    public static void main(String[] args) {
        //CMSEHR@cms.net.cn  Kangzhe2024  smtp.email.qq.com
        String tousers = "405930426@qq.com";
        String subject = "SMTP测试";

        SMTP.sendEmail(tousers, subject, "测试邮件发送多人。", true);
    }

    //ehr@jinhuijiu.com
    //zu$t61oFruf+1
//    public static String SMTPSERVER = "smtp.exmail.qq.com";
//    public static int SMTPPORT = 465;
//    public static String ACCOUT = "Amos.He@kayang.com";
//    public static String PWD = "Hey1992!@#";   //授权密码

    public static String SMTPSERVER = "smtp.jinhuijiu.com";
    public static int SMTPPORT = 465;
    public static String ACCOUT = "ehr@jinhuijiu.com";
    public static String PWD = "zu$t61oFruf+1";

//        public static String ACCOUT = "amos.he@kayang.com";
//    public static String PWD = "Hey1992!((@";   //授权密码

    //获取属性文件中的值,建议把配置的信息放到属性文件中,方便修改和获取

//    public static String SMTPSERVER = "smtp.bestway.cn"; //从属性文件中获取值其中key为smtp.server
//    public static String SMTPPORT = "25";   //端口号 465  465  465   不是456
//    public static String ACCOUT = "hr.notice@bestway.cn";//账户名:我的是163账户,此账户必须在设置中开启授权码授权
//    public static String PWD = "xgAp8nnb4C5jaPnm";   //授权密码
    //public static String PWD = "Bestway12345";


     //这里是发送给多个用户多个用户用都好分割xxx@xx.com,xxx@xx.com

    public static void sendEmail(String toUser, String subject, String content, boolean isssl) {
        try {
            // 创建邮件配置
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtps"); // 使用的协议（JavaMail规范要求）
            props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址
            props.setProperty("mail.smtp.port", SMTPPORT + "");
            props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
            props.setProperty("mail.smtp.ssl.enable", isssl ? "true" : "false");// 开启ssl
            if(isssl){
           //     props.put("mail.smtp.ssl.protocols", "TLSv1.2");
                props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory());
                props.put("mail.transport.protocol", "smtps");
                props.put("mail.smtp.socketFactory.fallback", "false");
                props.put("mail.smtp.socketFactory.port", SMTPPORT + "");
                System.out.println("This is SSL");
            }

            // 根据邮件配置创建会话，注意session别导错包
            Session session = Session.getDefaultInstance(props);
            // 开启debug模式，可以看到更多详细的输入日志
            session.setDebug(true);
            //创建邮件
            MimeMessage message = createEmail(session, toUser, subject, content);   //将用户和内容传递过来
            //获取传输通道
            Transport transport = session.getTransport();
            transport.connect(SMTPSERVER, ACCOUT, PWD);
            //连接，并发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static MimeMessage createEmail(Session session, String users, String subject, String content) throws Exception {
        // 根据会话创建邮件
        MimeMessage msg = new MimeMessage(session);
        // address邮件地址, personal邮件昵称, charset编码方式
        InternetAddress fromAddress = new InternetAddress(ACCOUT, subject, "utf-8");
        // 设置发送邮件方
        msg.setFrom(fromAddress);
//单个可以直接这样创建
//InternetAddress receiveAddress = new InternetAddress();
//设置邮件接收方
        InternetAddress[] internetAddressTo = new InternetAddress().parse(users);

//要被设置为 TO, CC 或者 BCC，这里 CC 代表抄送、BCC 代表秘密抄送。举例：Message.RecipientType.TO
        msg.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo);
        // 设置邮件标题
        msg.setSubject("测试标题", "utf-8");
        msg.setText(content);
        // 设置显示的发件时间
        msg.setSentDate(new Date());
        // 保存设置
        msg.saveChanges();
        return msg;
    }
}
